<script setup lang="ts">
import { getGuessGoods, getHomeBanner, getHomeHotMutli } from '@/services/home'
import NavBar from './components/NavBar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import type { BannerItem, GuessItem, hotItem } from '@/types/home'
import type { XtxGuessInstance } from '@/types/component'
import PageSkeleton from './components/PageSkeleton.vue'
import { useGuessList } from '@/composables'

const bannerList = ref<BannerItem[]>([])
// 请求获取轮播图数据
const getBannerData = async () => {
  const res = await getHomeBanner({ distributionSite: 1 })
  bannerList.value = res.result
}

const hotList = ref<hotItem[]>([])
// 获取热门推荐数据
const getHotData = async () => {
  const res = await getHomeHotMutli({})
  hotList.value = res.result
}

const { guessRef, onScrolltolower } = useGuessList()

// 下拉刷新状态
const triggered = ref(false)
// 下拉刷新
const onRefresherrefresh = async () => {
  triggered.value = true
  // 重置猜你喜欢组件数据
  guessRef.value?.resetGuessList()
  await Promise.all([getBannerData(), getHotData(), guessRef.value?.getMore()])
  triggered.value = false
}

// 判断数据是否加载中
const isLoading = ref(false)

onLoad(async () => {
  isLoading.value = true
  await Promise.all([getBannerData(), getHotData()])
  isLoading.value = false
})
</script>

<template>
  <NavBar></NavBar>
  <scroll-view
    class="scrollView"
    scroll-y
    @scrolltolower="onScrolltolower"
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="triggered"
  >
    <PageSkeleton v-if="isLoading"></PageSkeleton>
    <template v-else>
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <CategoryPanel></CategoryPanel>
      <HotPanel :list="hotList"></HotPanel>
      <XtxGuess ref="guessRef"></XtxGuess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #f7f7f8;
}
.scrollView {
  flex: 1;
}
</style>
